/**
 * 
 */
package org.jenkon.command;

import java.util.ArrayList;
import java.util.List;

/**
 * composite command for execute delegating commands in a composite pattern.<br>
 * 
 * @author Jens Konopka
 * 
 */
public class CompositeCommand implements Command {

	/**
	 * 
	 */
	private static final long serialVersionUID = -539257465726136578L;

	private final List<Command> delegates = new ArrayList<Command>();

	/**
	 * Default initialize constructor.
	 */
	public CompositeCommand() {
		this(new ArrayList<Command>());
	}

	/**
	 * Add {@link List} with delegating {@link Command} objects to the
	 * executable {@link List} with {@link Command} objects.
	 * 
	 * @param delegates
	 *            delegating {@link List} with {@link Command} objects
	 */
	public CompositeCommand(final List<Command> delegates) {
		this.add(delegates);
	}

	/**
	 * Add delegating {@link Command} to the executable {@link List} with
	 * {@link Command} objects.
	 * 
	 * @param delegate
	 *            add delegating command to executable commands
	 */
	public CompositeCommand(final Command delegate) {
		this.add(delegate);
	}

	/**
	 * Add {@link List} with delegating {@link Command} objects to the
	 * executable {@link List} with {@link Command} objects.
	 * 
	 * @param delegates
	 *            delegating {@link List} with {@link Command} objects
	 */
	public void add(final List<Command> delegates) {
		this.delegates.addAll(delegates);
	}

	/**
	 * Add delegating {@link Command} to the executable {@link List} with
	 * {@link Command} objects.
	 * 
	 * @param delegate
	 *            add delegating command to executable commands
	 */
	public void add(final Command delegate) {
		this.delegates.add(delegate);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxcommons.lang.Command#execute()
	 */
	public void execute() throws Exception {
		for (final Command command : this.delegates) {
			command.execute();
		}
	}

}
